/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmpresaGUI.java
 *
 * Created on 10/02/2011, 15:37:32
 */
package dio.com.gui;

import dio.com.controller.EmpresaController;
import dio.com.model.bean.Atividade;
import dio.com.model.bean.Empresa;
import dio.com.model.bean.Tipo;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Diógenes
 */
public class CadastrarEmpresa extends javax.swing.JDialog {

    DefaultTableModel tmClientes = new DefaultTableModel(null, new String[]{"CNPJ", "Nome", "Endereco", "Telefone"});
    ListSelectionModel lsmClientes;
    List<Empresa> listaEmpresa;
    Empresa empresa = new Empresa();
    Atividade atividade = new Atividade();
    Tipo tipo = new Tipo();
    String tipoCadastro;
    EmpresaController ec = new EmpresaController();

    /**
     * Creates new form EmpresaGUI
     */
    public CadastrarEmpresa(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
    }

    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        tfPesquisaCliente = new javax.swing.JTextField();
        btPesquisaCliente = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tfNome = new javax.swing.JTextField();
        lbEndereco = new javax.swing.JLabel();
        tfEndereco1 = new javax.swing.JLabel();
        tfEndereco = new javax.swing.JTextField();
        tfBairro = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        tfCidade = new javax.swing.JTextField();
        tfUF = new javax.swing.JTextField();
        tfIS = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tfTelefone = new javax.swing.JTextField();
        btNovo = new javax.swing.JButton();
        btAlterar = new javax.swing.JButton();
        btCancelar = new javax.swing.JButton();
        btExcluir = new javax.swing.JButton();
        btSalvar = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbEmpresas = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        tfCNPJ = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        tfTelefone1 = new javax.swing.JTextField();
        cbtipo = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        cbAtividade = new javax.swing.JComboBox();
        jLabel44 = new javax.swing.JLabel();
        tfPUK = new javax.swing.JTextField();
        jLabel43 = new javax.swing.JLabel();
        tfPIN = new javax.swing.JTextField();
        jLabel42 = new javax.swing.JLabel();
        tfNumeroContrato = new javax.swing.JTextField();
        jLabel41 = new javax.swing.JLabel();
        tfCodigo = new javax.swing.JTextField();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setTitle("GED - Cadastro de Empresas");

        btPesquisaCliente.setText("Pesquisar");
        btPesquisaCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisaClienteActionPerformed(evt);
            }
        });

        jLabel1.setText("Nome:");

        tfNome.setEditable(false);
        tfNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfNomeActionPerformed(evt);
            }
        });

        lbEndereco.setText("Endereço:");

        tfEndereco1.setText("Bairro:");

        tfEndereco.setEditable(false);
        tfEndereco.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfEnderecoActionPerformed(evt);
            }
        });

        tfBairro.setEditable(false);

        jLabel2.setText("Cidade:");

        jLabel3.setText("UF:");

        jLabel4.setText("IE:");

        tfCidade.setEditable(false);

        tfUF.setEditable(false);

        tfIS.setEditable(false);

        jLabel5.setText("Telefone:");

        tfTelefone.setEditable(false);

        btNovo.setText("Novo");
        btNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoActionPerformed(evt);
            }
        });

        btAlterar.setText("Alterar");
        btAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAlterarActionPerformed(evt);
            }
        });

        btCancelar.setText("Cancelar");
        btCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelarActionPerformed(evt);
            }
        });

        btExcluir.setText("Excluir");
        btExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btExcluirActionPerformed(evt);
            }
        });

        btSalvar.setText("Salvar");
        btSalvar.setEnabled(false);
        btSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSalvarActionPerformed(evt);
            }
        });

        tbEmpresas.setModel(tmClientes);
        tbEmpresas.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        lsmClientes = tbEmpresas.getSelectionModel();
        lsmClientes.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent e){
                if(!e.getValueIsAdjusting()){
                    tbClientesLinhaSelecionada(tbEmpresas);
                }
            }
        });
        jScrollPane2.setViewportView(tbEmpresas);

        jLabel6.setText("Nome:");

        jLabel7.setText("Email:");

        tfEmail.setEditable(false);

        jLabel8.setText("CNPJ:");

        tfCNPJ.setEditable(false);
        tfCNPJ.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfCNPJActionPerformed(evt);
            }
        });

        jLabel9.setText("Telefone:");

        tfTelefone1.setEditable(false);

        cbtipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-","Lucro Real","Lucro Presumido","Simples Nacional" }));
        cbtipo.setEnabled(false);

        jLabel10.setText("Tipo:");

        jLabel11.setText("Atividade:");

        cbAtividade.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-","Comercial", "Indústrial", "Serviços" }));
        cbAtividade.setEnabled(false);
        cbAtividade.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbAtividadeActionPerformed(evt);
            }
        });

        jLabel44.setText("PUK:");

        tfPUK.setEditable(false);

        jLabel43.setText("PIN:");

        tfPIN.setEditable(false);

        jLabel42.setText("Número do Contrato:");

        tfNumeroContrato.setEditable(false);

        jLabel41.setText("Código:");

        tfCodigo.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfPesquisaCliente)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addComponent(btPesquisaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(tfNome, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lbEndereco)
                                        .addGap(316, 316, 316))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(tfEndereco)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(tfBairro)
                                    .addComponent(tfEndereco1)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(tfEmail))
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfUF, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfIS))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfCNPJ)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfTelefone))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel9)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(tfTelefone1))))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(jLabel11)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(cbAtividade, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel10)
                                        .addGap(18, 18, 18)
                                        .addComponent(cbtipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addComponent(btNovo)
                                        .addGap(18, 18, 18)
                                        .addComponent(btAlterar)
                                        .addGap(18, 18, 18)
                                        .addComponent(btCancelar)
                                        .addGap(18, 18, 18)
                                        .addComponent(btExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(btSalvar)))
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addGap(27, 27, 27))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(tfCodigo, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel41, javax.swing.GroupLayout.Alignment.LEADING))
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel42, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(tfNumeroContrato, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel43)
                            .addComponent(tfPIN, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel44)
                            .addComponent(tfPUK, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btAlterar, btCancelar, btExcluir, btNovo, btSalvar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfPesquisaCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btPesquisaCliente))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel6)))
                .addGap(18, 18, 18)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cbtipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel11)
                        .addComponent(cbAtividade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel41)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel42)
                            .addComponent(jLabel43))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfNumeroContrato, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tfPIN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel44)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfPUK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lbEndereco)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(tfEndereco1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfBairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tfCidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tfTelefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(tfUF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(tfIS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(tfCNPJ, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(tfTelefone1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNovo)
                    .addComponent(btAlterar)
                    .addComponent(btCancelar)
                    .addComponent(btExcluir)
                    .addComponent(btSalvar))
                .addGap(27, 27, 27))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btAlterar, btCancelar, btExcluir, btNovo, btPesquisaCliente});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    protected void tbClientesLinhaSelecionada(JTable tb) {
        if (tb.getSelectedRow() != -1) {
            tfNome.setText(listaEmpresa.get(tb.getSelectedRow()).getNome());
            tfEndereco.setText(listaEmpresa.get(tb.getSelectedRow()).getRua());
            tfCidade.setText(listaEmpresa.get(tb.getSelectedRow()).getCidade());
            tfBairro.setText(listaEmpresa.get(tb.getSelectedRow()).getBairro());
            tfUF.setText(listaEmpresa.get(tb.getSelectedRow()).getUf());
            tfTelefone.setText(listaEmpresa.get(tb.getSelectedRow()).getTelefone());
            tfTelefone1.setText(listaEmpresa.get(tb.getSelectedRow()).getTelefone1());
            tfIS.setText(listaEmpresa.get(tb.getSelectedRow()).getIs());
            tfCNPJ.setText(listaEmpresa.get(tb.getSelectedRow()).getCnpj());
            tfEmail.setText(listaEmpresa.get(tb.getSelectedRow()).getEmail());
            empresa.setId(listaEmpresa.get(tb.getSelectedRow()).getId());
            cbtipo.setSelectedIndex(listaEmpresa.get(tb.getSelectedRow()).getTipo().getId());
            cbAtividade.setSelectedIndex(listaEmpresa.get(tb.getSelectedRow()).getAtividade().getId());
            tfCodigo.setText(listaEmpresa.get(tb.getSelectedRow()).getCodigo());
            tfNumeroContrato.setText(listaEmpresa.get(tb.getSelectedRow()).getNum_contrato());
            tfPIN.setText(listaEmpresa.get(tb.getSelectedRow()).getPin());
            tfPUK.setText(listaEmpresa.get(tb.getSelectedRow()).getPuk());
                    
        } else {
            tfNome.setText("");
            tfEndereco.setText("");
            tfIS.setText("");
            tfCidade.setText("");
            tfBairro.setText("");
            tfUF.setText("");
            tfTelefone.setText("");
            tfTelefone1.setText("");
            tfCNPJ.setText("");
            tfEmail.setText("");
            tfCodigo.setText("");
            tfNumeroContrato.setText("");
            tfPIN.setText("");
            tfPUK.setText("");
        }
    }

    protected void cadastrarEmpresa() {
        if (verificarCampos() && verificarUF()) {
            empresa.setNome(tfNome.getText().trim());
            empresa.setRua(tfEndereco.getText().trim());
            empresa.setBairro(tfBairro.getText().trim());
            empresa.setCidade(tfCidade.getText().trim());
            empresa.setUf(tfUF.getText().trim());
            empresa.setTelefone(tfTelefone.getText().trim());
            empresa.setTelefone1(tfTelefone1.getText().trim());
            empresa.setIs(tfIS.getText().trim());
            empresa.setCnpj(tfCNPJ.getText().trim());
            empresa.setEmail(tfEmail.getText().trim());
            
            atividade.setId(cbAtividade.getSelectedIndex());
            tipo.setId(cbtipo.getSelectedIndex());
            
            empresa.setAtividade(atividade);
            empresa.setTipo(tipo);

            empresa.setCodigo(tfCodigo.getText());
            empresa.setNum_contrato(tfNumeroContrato.getText());
            empresa.setPin(tfPIN.getText());
            empresa.setPuk(tfPUK.getText());
            
            
            //>>>>>>>>INSERT COM JDBC<<<<<<<<

            ec.cadastrarEmpresa(empresa);

            /*
             * empresaDAO.salvar(empresa);
             * Principal.session.getTransaction().commit();
             * Principal.session.getTransaction().begin(); desabilitarCampos();
            btPesquisaCliente.doClick();
             */
        }
    }

    protected void listarEmpresa() {

        listaEmpresa = ec.listarEmpresas();
        mostrarEmpresa(listaEmpresa);
    }

    protected void mostrarEmpresa(List<Empresa> Empresas) {
        while (tmClientes.getRowCount() > 0) {
            tmClientes.removeRow(0);
        }

        if (Empresas.isEmpty() || Empresas == null) {
            JOptionPane.showMessageDialog(this, "Nenhum empresa encontrado!");
        } else {
            String[] campos = new String[]{null, null, null, null};
            for (int i = 0; i < Empresas.size(); i++) {
                tmClientes.addRow(campos);
                tmClientes.setValueAt(Empresas.get(i).getCnpj(), i, 0);
                tmClientes.setValueAt(Empresas.get(i).getNome(), i, 1);
                tmClientes.setValueAt(Empresas.get(i).getRua(), i, 2);
                tmClientes.setValueAt(Empresas.get(i).getTelefone(), i, 3);

            }
        }
    }

    private void btPesquisaClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisaClienteActionPerformed
        listarEmpresa();
    }//GEN-LAST:event_btPesquisaClienteActionPerformed

    private void tfNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfNomeActionPerformed
        btAlterar.setEnabled(false);
    }//GEN-LAST:event_tfNomeActionPerformed

    private void tfEnderecoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfEnderecoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfEnderecoActionPerformed

    protected void habilitarCampos() {
        tfNome.setEditable(true);
        tfEndereco.setEditable(true);
        tfBairro.setEditable(true);
        tfCidade.setEditable(true);
        tfIS.setEditable(true);
        cbtipo.setEnabled(true);
        cbAtividade.setEnabled(true);
        tfUF.setEditable(true);
        tfTelefone.setEditable(true);
        tfTelefone1.setEditable(true);
        tfEmail.setEditable(true);
        tfCNPJ.setEditable(true);
        tfPesquisaCliente.setEditable(false);
        btPesquisaCliente.setEnabled(false);
        btSalvar.setEnabled(true);
        
        tfCodigo.setEditable(true);
        tfNumeroContrato.setEditable(true);
        tfPIN.setEditable(true);
        tfPUK.setEditable(true);
                

    }

    protected void desabilitarCampos() {
        tfNome.setEditable(false);
        tfEndereco.setEditable(false);
        tfBairro.setEditable(false);
        cbtipo.setEnabled(false);
        cbAtividade.setEnabled(false);
        tfCidade.setEditable(false);
        tfIS.setEditable(false);
        tfUF.setEditable(false);
        tfTelefone.setEditable(false);
        tfTelefone1.setEditable(false);
        tfEmail.setEditable(false);
        tfCNPJ.setEditable(false);
        tfPesquisaCliente.setEditable(true);
        btPesquisaCliente.setEnabled(true);
        btSalvar.setEnabled(false);

        tfCodigo.setEditable(false);
        tfNumeroContrato.setEditable(false);
        tfPIN.setEditable(false);
        tfPUK.setEditable(false);
    }

    protected boolean verificarCampos() {
        if (!tfNome.getText().trim().equals("")) {
            return true;
        }
        JOptionPane.showMessageDialog(this, "O campo Nome é obrigatório","Alerta!",JOptionPane.WARNING_MESSAGE);
        return false;
    }

    private void novoCliente() {
        habilitarCampos();
        tfNome.setText("");
        tfEndereco.setText("");
        tfIS.setText("");
        tfCidade.setText("");
        tfBairro.setText("");
        tfUF.setText("");
        tfTelefone.setText("");
        tfTelefone1.setText("");
        tfCNPJ.setText("");
        tfEmail.setText("");
        tfCodigo.setText("");
        tfNumeroContrato.setText("");
        tfPIN.setText("");
        tfPUK.setText("");
    }

    private void btNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoActionPerformed
        tipoCadastro = "novo";
        novoCliente();
        btAlterar.setEnabled(false);
        tfNome.requestFocus();
    }//GEN-LAST:event_btNovoActionPerformed

    protected void alteraCliente() {
        if (tbEmpresas.getSelectedRow() != -1) {
            habilitarCampos();
        } else {
            JOptionPane.showMessageDialog(this, "Selecione um empresa!");
        }
    }

    protected void alterarCliente() {
        if (verificarCampos() && verificarUF()) {
            empresa.setNome(tfNome.getText().trim());
            empresa.setRua(tfEndereco.getText().trim());
            empresa.setBairro(tfBairro.getText().trim());
            empresa.setCidade(tfCidade.getText().trim());
            empresa.setUf(tfUF.getText().trim());
            empresa.setIs(tfIS.getText().trim());
            empresa.setTelefone(tfTelefone.getText().trim());
            empresa.setTelefone1(tfTelefone1.getText().trim());
            empresa.setCnpj(tfCNPJ.getText().trim());
            empresa.setEmail(tfEmail.getText().trim());
            atividade.setId(cbAtividade.getSelectedIndex());
            tipo.setId(cbtipo.getSelectedIndex());
            empresa.setAtividade(atividade);
            empresa.setTipo(tipo);
            empresa.setCodigo(tfCodigo.getText().trim());
            empresa.setNum_contrato(tfNumeroContrato.getText().trim());
            empresa.setPin(tfPIN.getText().trim());
            empresa.setPuk(tfPUK.getText().trim());
            //>>>>>>>>UPDATE COM JDBC<<<<<<<<
            ec.alterarEmpresa(empresa);
            desabilitarCampos();
            
            btPesquisaCliente.doClick();

        } else {
            JOptionPane.showMessageDialog(this, "Informe o nome do empresa!");
            tfNome.requestFocus();
        }
    }

    private void btAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAlterarActionPerformed
        tipoCadastro = "alteracao";
        alteraCliente();
        btNovo.setEnabled(false);
        tbEmpresas.setEnabled(false);
    }//GEN-LAST:event_btAlterarActionPerformed

    private void btCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelarActionPerformed
        desabilitarCampos();
        btNovo.setEnabled(true);
        tbEmpresas.setEnabled(true);
        btAlterar.setEnabled(true);
    }//GEN-LAST:event_btCancelarActionPerformed

    protected boolean verificarUF() {
        if (tfUF.getText().length() > 2) {
            return false;
        }
        return true;
    }
    private void btSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSalvarActionPerformed
        if (tipoCadastro.equals("novo")) {
            cadastrarEmpresa();
            btNovo.setEnabled(true);
            tbEmpresas.setEnabled(true);
            btAlterar.setEnabled(true);
            desabilitarCampos();
            btPesquisaCliente.doClick();
        } else if (tipoCadastro.equals("alteracao")) {
            alterarCliente();
            btNovo.setEnabled(true);
            tbEmpresas.setEnabled(true);
            btAlterar.setEnabled(true);
            desabilitarCampos();
            btSalvar.setEnabled(true);
            btPesquisaCliente.doClick();
        }
    }//GEN-LAST:event_btSalvarActionPerformed

    protected void excluirCliente() {
        if (tbEmpresas.getSelectedRow() != -1) {
            int resposta = JOptionPane.showConfirmDialog(this, "Deseja excluir a empresa selecionada?", "Confirmação", JOptionPane.YES_NO_CANCEL_OPTION);

            if (resposta == JOptionPane.YES_OPTION) {
                for (Empresa c : this.listaEmpresa) {
                    if (listaEmpresa.size() > 0) {
                        if (c.getCnpj() == listaEmpresa.get(tbEmpresas.getSelectedRow()).getCnpj()) {
                            empresa = c;
                            ec.excluirEmpresa(empresa);
                            btPesquisaCliente.doClick();
                        }
                    }
                }
                //Produtos produto = this.produtos.get(produtos.get(tbProdutos.getSelectedRow()).getCodigo());
            }
        }
    }
    private void btExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExcluirActionPerformed
        excluirCliente();
    }//GEN-LAST:event_btExcluirActionPerformed

    private void tfCNPJActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfCNPJActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfCNPJActionPerformed

    private void cbAtividadeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbAtividadeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbAtividadeActionPerformed
    /*
     * @param args the command line arguments
     *
     * public static void main(String args[]) {
     * java.awt.EventQueue.invokeLater(new Runnable() {
     *
     * public void run() { new EmpresaGUI(session).setVisible(true); } });
    }
     */
/*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CadastrarEmpresa1 dialog = new CadastrarEmpresa1(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAlterar;
    private javax.swing.JButton btCancelar;
    private javax.swing.JButton btExcluir;
    private javax.swing.JButton btNovo;
    public javax.swing.JButton btPesquisaCliente;
    private javax.swing.JButton btSalvar;
    private javax.swing.JComboBox cbAtividade;
    private javax.swing.JComboBox cbtipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel41;
    private javax.swing.JLabel jLabel42;
    private javax.swing.JLabel jLabel43;
    private javax.swing.JLabel jLabel44;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lbEndereco;
    protected javax.swing.JTable tbEmpresas;
    protected javax.swing.JTextField tfBairro;
    private javax.swing.JTextField tfCNPJ;
    protected javax.swing.JTextField tfCidade;
    private javax.swing.JTextField tfCodigo;
    private javax.swing.JTextField tfEmail;
    protected javax.swing.JTextField tfEndereco;
    private javax.swing.JLabel tfEndereco1;
    protected javax.swing.JTextField tfIS;
    protected javax.swing.JTextField tfNome;
    private javax.swing.JTextField tfNumeroContrato;
    private javax.swing.JTextField tfPIN;
    private javax.swing.JTextField tfPUK;
    protected javax.swing.JTextField tfPesquisaCliente;
    protected javax.swing.JTextField tfTelefone;
    protected javax.swing.JTextField tfTelefone1;
    protected javax.swing.JTextField tfUF;
    // End of variables declaration//GEN-END:variables
}
